{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d6166a94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开心购物\n",
      "---------【BOOK STORE】---------\n",
      "['编号', '《书籍名称》', '价格']\n",
      "['01', '《瓦尔登湖》', 68]\n",
      "['02', '《老人与海》', 88]\n",
      "['03', '《平凡的世界》', 99]\n",
      "['04', '《解忧杂货店》', 110]\n",
      "\n",
      "请输入您的余额：222\n",
      "---------【BOOK STORE】---------\n",
      "['编号', '《书籍名称》', '价格']\n",
      "['01', '《瓦尔登湖》', 68]\n",
      "['02', '《老人与海》', 88]\n",
      "['03', '《平凡的世界》', 99]\n",
      "['04', '《解忧杂货店》', 110]\n",
      "\n",
      "请输入您想要购买的书籍编号(亦输入“q”退出购买)：4\n",
      "《解忧杂货店》 已经添加到您的购物车，您的余额还有 112 元\n",
      "---------【BOOK STORE】---------\n",
      "['编号', '《书籍名称》', '价格']\n",
      "['01', '《瓦尔登湖》', 68]\n",
      "['02', '《老人与海》', 88]\n",
      "['03', '《平凡的世界》', 99]\n",
      "['04', '《解忧杂货店》', 110]\n",
      "\n",
      "请输入您想要购买的书籍编号(亦输入“q”退出购买)：3\n",
      "《平凡的世界》 已经添加到您的购物车，您的余额还有 13 元\n",
      "---------【BOOK STORE】---------\n",
      "['编号', '《书籍名称》', '价格']\n",
      "['01', '《瓦尔登湖》', 68]\n",
      "['02', '《老人与海》', 88]\n",
      "['03', '《平凡的世界》', 99]\n",
      "['04', '《解忧杂货店》', 110]\n",
      "\n",
      "请输入您想要购买的书籍编号(亦输入“q”退出购买)：q\n",
      "----------------- \n",
      " 您的购物车已放入：[['04', '《解忧杂货店》', 110], ['03', '《平凡的世界》', 99]]  \n",
      " 您的余额还有：13 元\n",
      "书籍是人类进步的阶梯，谢谢惠顾~\n"
     ]
    }
   ],
   "source": [
    "# 1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "# 商品列表\n",
    "\n",
    "\n",
    "print(\"欢迎登录开心购物系统\")\n",
    "user_id = [\"guosiwen\", \"liliumang\", \"jianglaoban\"]\n",
    "password_id = [\"888\", \"666\", \"999\"]\n",
    "# 建立数据库\n",
    "\n",
    "username = input(\"请输入您的账号：\")\n",
    "password = input(\"请输入您的密码：\")\n",
    "\n",
    "if username in user_id:\n",
    "    if password in password_id:\n",
    "        print(\"欢迎\", username, \"来到shopping之都\")\n",
    "    else:\n",
    "        print(\"请确认账号密码是否正确\")\n",
    "\n",
    "\n",
    "    goods_list = [\n",
    "        # 商品1 商品名称，商品价格，商品编号...\n",
    "        # 商品模板 ： [商品名称,商品名称]\n",
    "        [\"编号\", \"《书籍名称》\", \"价格\"],\n",
    "        [\"01\", \"《瓦尔登湖》\", 68],\n",
    "        [\"02\", \"《老人与海》\", 88],\n",
    "        [\"03\", \"《平凡的世界》\", 99],\n",
    "        [\"04\", \"《解忧杂货店》\", 110],\n",
    "    ]\n",
    "\n",
    "    info = '''---------【BOOK STORE】---------\n",
    "    %s\n",
    "    %s\n",
    "    %s\n",
    "    %s\n",
    "    %s\n",
    "    ''' % (goods_list[0], goods_list[1], goods_list[2], goods_list[3], goods_list[4])\n",
    "    print(info)\n",
    "\n",
    "    # 不是所有的代码都可以一次性考虑全面！例如这里应该写一个用户商品列表\n",
    "    user_shopping_car = []  # append 可以新增列表\n",
    "\n",
    "    # 用户输入： input\n",
    "    Alipay = int(input(\"您的小金库还有：\"))\n",
    "\n",
    "    # 打印商品列表 （规范打印，样式是先写的，然后再输入数据）\n",
    "    # 先设计模板样式： 商品编号：xx，商品名称：xx，商品的价格：xx 元\n",
    "    while True:\n",
    "        #     for i in range(len(goods_list)):\n",
    "        print(info)\n",
    "    # 这里len（goods_list）= 5 用len（goods_list）的话可以系统自动取goods_list的长度，5是goods_list的长度\n",
    "    #  print(\"商品编号：%s，商品名称：%s，商品的价格：%d 元\"%(0,goods_list[0][0],goods_list[0][1]))\n",
    "    #     用0和i的区别是：0的话只执行第一栏，i的话可以执行整一栏\n",
    "    #         print(\"商品编号：%s，商品名称：%s，商品的价格：%d 元\" % (i, goods_list[i][0], goods_list[i][1]))\n",
    "\n",
    "        # 2、让用户输入商品编号进行商品的购买。\n",
    "        choice = input(\"请输入您想要购买的书籍编号(亦输入“q”退出购买)：\")  # 用户输入可能不是数值，也可能是\n",
    "    # if choice .(.的意思是“属于”或者“是在”)isdigit（指的是变成数据）（）：\n",
    "        if choice.isdigit():\n",
    "            # 用int转换数据类型为数值\n",
    "            choice = int(choice)\n",
    "            # 是正确的编号\n",
    "            if choice < len(goods_list):  # 商品的信息：goods_list[choice] ，商品名称：goods_list[choice][0]  商品价格:goods_list[choice][1]\n",
    "                # 1. 看一看用户钱够不够； Alipay VS goods_list[choice][1]\n",
    "                if Alipay < goods_list[choice][2]:\n",
    "                    print(\"您的余额不够，请更换支付方式\")\n",
    "                # 2. 如果够的话，赶紧扣钱！把商品加到购物车\n",
    "                else:\n",
    "                    Alipay = Alipay - int(goods_list[choice][2])\n",
    "                    user_shopping_car.append(goods_list[choice])\n",
    "                    print(goods_list[choice][1], \"已经添加到您的购物车，您的余额还有\", Alipay, \"元\")\n",
    "                    # 提示用户：本次购买成功\n",
    "                    # print(\"goods_list[choice][1]\", \"已经添加到您的购物车，您的余额还有\", \"【%goods_list[choice][2]】\")\n",
    "\n",
    "            # 不是正确的编号\n",
    "            else:\n",
    "                print(\"您输入的商品不存在，请输入正确的编号\")\n",
    "        elif choice == \"q\":\n",
    "            print(\"-----------------\", '\\n', \"您的购物车已放入：%s \" % (user_shopping_car), '\\n', \"您的余额还有：%d 元\" % (Alipay))\n",
    "            print(\"书籍是人类进步的阶梯，谢谢惠顾~\")\n",
    "            break\n",
    "            # 两种情况：\n",
    "            # 1. 按照我们的要求输入的文本，例如：输入q表示quit退出的意思\n",
    "\n",
    "            # 2. 输入的不符合我们的要求的文本\n",
    "        else:\n",
    "            print(\"请输入正确的商品编号！！或者输入“q”退出\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c689fda5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
